package com.example.demo.fruits;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;


@Slf4j
@Data
public class Fruits {

    private int unitPrice;

    private int weight;

    private Double discount;

    public Fruits(int unitPrice,int weight){
        this.unitPrice=unitPrice;
        this.weight=weight;
    }

    public Fruits(int unitPrice,int weight,double discount){
        this.unitPrice=unitPrice;
        this.weight=weight;
        this.discount=discount;
    }

    public Fruits(){

    }

    public double sum(){
        if(this.unitPrice<0){
            log.error("价格输入异常");
        }
        if(this.weight<0){
            log.error("重量输入异常");
        }
        BigDecimal multiply=null;
        BigDecimal b=new BigDecimal(this.unitPrice*this.weight);
        if(discount!=null){
            multiply = b.multiply(new BigDecimal(this.discount));
        }

        if(multiply!=null){
            return multiply.doubleValue();
        }

        return b.doubleValue();
    }
}
